{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ee4482d6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<html>\n",
      " <head>\n",
      "  <title>\n",
      "   This is a python demo page\n",
      "  </title>\n",
      " </head>\n",
      " <body>\n",
      "  <p class=\"title\">\n",
      "   <b>\n",
      "    The demo python introduces several python courses.\n",
      "   </b>\n",
      "  </p>\n",
      "  <p class=\"course\">\n",
      "   Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\n",
      "   <a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">\n",
      "    Basic Python\n",
      "   </a>\n",
      "   and\n",
      "   <a class=\"py2\" href=\"http://www.icourse163.org/course/BIT-1001870001\" id=\"link2\">\n",
      "    Advanced Python\n",
      "   </a>\n",
      "   .\n",
      "  </p>\n",
      " </body>\n",
      "</html>\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "r=requests.get(\"http://python123.io/ws/demo.html\")\n",
    "r.text\n",
    "demo=r.text\n",
    "from bs4 import BeautifulSoup\n",
    "soup=BeautifulSoup(demo,\"html.parser\")\n",
    "print(soup.prettify())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6692309e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "soup=BeautifulSoup(demo,\"html.parser\")\n",
    "soup.a.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "63939ebe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'p'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "soup=BeautifulSoup(demo,\"html.parser\")\n",
    "soup.a.parent.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b5b3d21f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'href': 'http://www.icourse163.org/course/BIT-268001',\n",
       " 'class': ['py1'],\n",
       " 'id': 'link1'}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "soup=BeautifulSoup(demo,\"html.parser\")\n",
    "tag=soup.a\n",
    "tag.attrs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3204c7fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The demo python introduces several python courses.'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "soup.p.string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "becfdb74",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<head><title>This is a python demo page</title></head>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "soup.head"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4e3f7494",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(soup.body.contents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d32b325d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "soup.body.contents[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0aac5c9c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "<p class=\"title\"><b>The demo python introduces several python courses.</b></p>\n",
      "<b>The demo python introduces several python courses.</b>\n",
      "The demo python introduces several python courses.\n",
      "\n",
      "\n",
      "<p class=\"course\">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n",
      "<a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">Basic Python</a> and <a class=\"py2\" href=\"http://www.icourse163.org/course/BIT-1001870001\" id=\"link2\">Advanced Python</a>.</p>\n",
      "Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n",
      "\n",
      "<a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">Basic Python</a>\n",
      "Basic Python\n",
      " and \n",
      "<a class=\"py2\" href=\"http://www.icourse163.org/course/BIT-1001870001\" id=\"link2\">Advanced Python</a>\n",
      "Advanced Python\n",
      ".\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for child in soup.body.descendants:\n",
    "    print(child)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "862cf0a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<head><title>This is a python demo page</title></head>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "soup.title.parent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d8389765",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<html><head><title>This is a python demo page</title></head>\n",
       "<body>\n",
       "<p class=\"title\"><b>The demo python introduces several python courses.</b></p>\n",
       "<p class=\"course\">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\n",
       "<a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">Basic Python</a> and <a class=\"py2\" href=\"http://www.icourse163.org/course/BIT-1001870001\" id=\"link2\">Advanced Python</a>.</p>\n",
       "</body></html>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "soup.html.parent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ce2acee4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "' and '"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "soup=BeautifulSoup(demo,\"html.parser\")\n",
    "soup.a.next_sibling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f4398532",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\\r\\n'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "soup.a.previous_sibling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "70c67507",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " and \n",
      "<a class=\"py2\" href=\"http://www.icourse163.org/course/BIT-1001870001\" id=\"link2\">Advanced Python</a>\n",
      ".\n"
     ]
    }
   ],
   "source": [
    "for sibling in soup.a.next_siblings:\n",
    "    print(sibling)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "3dda692f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "soup=BeautifulSoup(demo,\"html.parser\")\n",
    "for link in soup.find_all(\"a\"):\n",
    "    print(link.get(\"hret\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c76d21c1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">Basic Python</a>, <a class=\"py2\" href=\"http://www.icourse163.org/course/BIT-1001870001\" id=\"link2\">Advanced Python</a>]\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "r=requests.get(\"http://python123.io/ws/demo.html\")\n",
    "demo=r.text\n",
    "from bs4 import BeautifulSoup\n",
    "soup=BeautifulSoup(demo,\"html.parser\")\n",
    "print(soup.find_all(\"a\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "4880a58a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<b>The demo python introduces several python courses.</b>, <a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">Basic Python</a>, <a class=\"py2\" href=\"http://www.icourse163.org/course/BIT-1001870001\" id=\"link2\">Advanced Python</a>]\n"
     ]
    }
   ],
   "source": [
    "print(soup.find_all([\"a\",\"b\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "14001bf0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<html><head><title>This is a python demo page</title></head>\n",
      "<body>\n",
      "<p class=\"title\"><b>The demo python introduces several python courses.</b></p>\n",
      "<p class=\"course\">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n",
      "<a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">Basic Python</a> and <a class=\"py2\" href=\"http://www.icourse163.org/course/BIT-1001870001\" id=\"link2\">Advanced Python</a>.</p>\n",
      "</body></html>, <head><title>This is a python demo page</title></head>, <title>This is a python demo page</title>, <body>\n",
      "<p class=\"title\"><b>The demo python introduces several python courses.</b></p>\n",
      "<p class=\"course\">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n",
      "<a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">Basic Python</a> and <a class=\"py2\" href=\"http://www.icourse163.org/course/BIT-1001870001\" id=\"link2\">Advanced Python</a>.</p>\n",
      "</body>, <p class=\"title\"><b>The demo python introduces several python courses.</b></p>, <b>The demo python introduces several python courses.</b>, <p class=\"course\">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\r\n",
      "<a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">Basic Python</a> and <a class=\"py2\" href=\"http://www.icourse163.org/course/BIT-1001870001\" id=\"link2\">Advanced Python</a>.</p>, <a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">Basic Python</a>, <a class=\"py2\" href=\"http://www.icourse163.org/course/BIT-1001870001\" id=\"link2\">Advanced Python</a>]\n"
     ]
    }
   ],
   "source": [
    "print(soup.find_all())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57243d5c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d1f7bee",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
